/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.campaign_targeting_service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

import com.hiddenequity.utils.City;
import com.hiddenequity.utils.Country;
import com.hiddenequity.utils.Province;
import com.hiddenequity.utils.SOAPUtils;
import com.hiddenequity.utils.UrlBuncher;

public class CampaignTargetingService {
	
	public static final String URL_CAMPAIGN_TARGET_SERVICE = UrlBuncher.URL_BASE + "CampaignTargetService";

	/*
	 * Set language target
	 */
	static public String setLangTarget(String campaignId, ArrayList<String> languageCode){
		String SOAPBody = 
			"<soap:Body>" +
				"<mutate xmlns=" + SOAPUtils.XMLNS +
					"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
				"<operations>" +
					"<operator>SET</operator>" +
					"<operand xsi:type='LanguageTargetList'>" +
						"<campaignId>" + campaignId + "</campaignId>";

		Iterator<String> code = languageCode.iterator();
		
		while(code.hasNext()){
			SOAPBody = SOAPBody + "<targets xsi:type='LanguageTarget'>" +
					"<languageCode>" + code.next() +"</languageCode>" +
					"</targets>";;
		}

		SOAPBody = SOAPBody + "</operand>" +
				"</operations>" +
			"</mutate>" +
		"</soap:Body>";
		String envelope = SOAPUtils.buildEnvelope( SOAPBody);
		return envelope;
	}//setLangTarget
	
	
	
	/*
	 * Set Platform Target 
	 */
	static public String setPlatformTarget(String campaignId, boolean desktop, boolean mobile){//Add arguments
		String SOAPBody = 
				"<soap:Body>" +
					"<mutate xmlns=" + SOAPUtils.XMLNS +
						"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
					"<operations>" +
						"<operator>SET</operator>" +
						"<operand xsi:type='PlatformTargetList'>" +
							"<campaignId>" + campaignId + "</campaignId>";
	
		if(desktop && mobile){
			desktop = false;
			mobile = false;
		}
		
		if(desktop){
			SOAPBody = SOAPBody + 
				"<targets xsi:type='PlatformTarget'>"+
					"<platformType>DESKTOP</platformType>"+
				"</targets>";
		}
		
		if(mobile){
			SOAPBody = SOAPBody + 
				"<targets xsi:type='PlatformTarget'>"+
					"<platformType>HIGH_END_MOBILE</platformType>"+
				"</targets>";
		}
		
		SOAPBody = SOAPBody +"</operand>" +
					"</operations>" +
				"</mutate>" +
			"</soap:Body>";
		
		String envelope = SOAPUtils.buildEnvelope( SOAPBody);
		return envelope;
	}//setPlatformTarget
	
	
	
	/*
	 * Set Mobile Platform Target 
	 */
	static public String setMobilePlatformTarget(String campaignId, boolean Android, 
			boolean iPad, boolean iPhone, boolean Palm){//Add arguments
		String SOAPBody = 
				"<soap:Body>" +
					"<mutate xmlns=" + SOAPUtils.XMLNS +
						"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
						"<operations>" +
							"<operator>SET</operator>" +
							"<operand xsi:type='MobileTargetList'>" +
								"<campaignId>" + campaignId + "</campaignId>";
		
		if(Android && iPad && iPhone && Palm){
			Android = false;
			iPad = false;
			iPhone = false;
			Palm = false;
		}
		
		if(Android){
			SOAPBody = SOAPBody + 
					"<targets xsi:type='MobilePlatformTarget'>"+
						"<platformName>Android</platformName>" +
					"</targets>";
		}
			
		if(iPad){
			SOAPBody = SOAPBody + 
				"<targets xsi:type='MobilePlatformTarget'>"+
					"<platformName>iPad</platformName>" +
				"</targets>";
		}
			
		if(iPhone){
			SOAPBody = SOAPBody + 
				"<targets xsi:type='MobilePlatformTarget'>"+
					"<platformName>iPhone</platformName>" +
				"</targets>";
		}
			
		if(Palm){
			SOAPBody = SOAPBody + 
				"<targets xsi:type='MobilePlatformTarget'>"+
					"<platformName>Palm</platformName>" +
				"</targets>";
		}
			
		
		SOAPBody = SOAPBody + "</operand>" +
						"</operations>" +
					"</mutate>" +
				"</soap:Body>";
		String envelope = SOAPUtils.buildEnvelope( SOAPBody);
		return envelope;
	}//setMobilePlatformTarget
	
	
	
	/*
	 * Set geo target
	 */
	static public String setGeoTarget(String campaignId, ArrayList<Country> selectedCountry, 
			ArrayList<Province> selectedProvince, ArrayList<City> selectedCity){
		
		String countryTarget = "";
		if(!selectedCountry.isEmpty()){
			Collections.reverse(selectedCountry);
			for(Country country : selectedCountry){
				countryTarget = countryTarget +
						"<targets xsi:type='CountryTarget'>" +
							"<countryCode>" + country.getCode() + "</countryCode>" +
						"</targets>";				
			}
		}
		
		String provinceTarget = "";
		if(!selectedProvince.isEmpty()){
			for(Province province : selectedProvince){
				provinceTarget = provinceTarget +
						"<targets xsi:type='ProvinceTarget'>" +
							"<provinceCode>" + province.getProvinceCode() + "</provinceCode>" +
						"</targets>";				
			}

		}
		
		String cityTarget = "";
		if(!selectedCity.isEmpty()){
			
			for(City city : selectedCity){
				cityTarget = cityTarget +
						"<targets xsi:type='CityTarget'>" +
							"<cityName>" + city.getCityName() + "</cityName>" +
							"<provinceCode>" + city.getProvinceCode() + "</provinceCode>" +
							"<countryCode>" + city.getCountryCode() + "</countryCode>" +
						"</targets>";			
			}
		}
		
		String SOAPBody = 
				"<soap:Body>" +
					"<mutate xmlns=" + SOAPUtils.XMLNS +
						"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
						"<operations>" +
							"<operator>SET</operator>" +
							"<operand xsi:type='GeoTargetList'>" +
								"<campaignId>" + campaignId + "</campaignId>" +	
								countryTarget +
								provinceTarget +
								cityTarget + 
							"</operand>" +
						"</operations>" +
					"</mutate>" +
				"</soap:Body>";
		String envelope = SOAPUtils.buildEnvelope( SOAPBody);
		return envelope;
	}//setGeoTarget

	
	
	/*
	 * Get Campaign Target By Id
	 */
	static public String getCampaignTargetById(String campaignId){

		String SOAPBody =
				"<soap:Body>" +
					"<get>" +
						"<selector>" +
						"<campaignIds>" + campaignId + "</campaignIds>" +
						"</selector>" +
					"</get>" +
				"</soap:Body>";		
		
		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;
	}//getCampaignTargetById	

}
